package com.mioto.ld.module.feedback.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.feedback.model.FeedbackDetailAppVO;
import com.mioto.ld.module.feedback.model.FeedbackPagerVO;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.feedback.model.Feedback;
import com.mioto.ld.module.feedback.service.IFeedbackService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 *
 * @author mioto
 * @date 2024-05-14 14:54:42
 */
@RestController
@RequestMapping("feedback")
@Api(tags = "用户反馈管理")
public class FeedbackController {
    @Resource
    private IFeedbackService feedbackService;

    @GetMapping
    @ApiOperation(value="查询用户反馈列表",response = Feedback.class)
    public ResultData list (HttpServletRequest request,Feedback feedback){
        return ResultData.success(feedbackService.findList(feedback));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询用户反馈",response = FeedbackPagerVO.class)
    public ResultData pager (HttpServletRequest request, Feedback feedback,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<FeedbackPagerVO> list = feedbackService.findPager(feedback);
        PageInfo<FeedbackPagerVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名查询用户反馈",response = Feedback.class)
    public ResultData findByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return ResultData.success(feedbackService.findByColumn(StrUtil.toUnderlineCase(column),value));
    }

    @PostMapping
    @ApiOperation(value="新增用户反馈")
    public ResultData add (Feedback feedback){
        return Optional.of(feedbackService.insert(feedback))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(feedback))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新用户反馈 局部更新,忽略空值")
    public ResultData updateIgnoreNull (Feedback feedback){
        return Optional.of(feedbackService.updateIgnoreNull(feedback))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(feedback))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @PutMapping
    @ApiOperation(value="更新用户反馈 整体更新")
    public ResultData update (Feedback feedback){
        return Optional.of(feedbackService.update(feedback))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(feedback))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除用户反馈")
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(feedbackService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除用户反馈")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(feedbackService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @GetMapping("/app/detail")
    @ApiOperation(value="app端反馈详情",response = FeedbackDetailAppVO.class)
    public ResultData findDetailToApp (Integer feedbackId){
        return ResultData.success(feedbackService.findDetailToApp(feedbackId));
    }
}